import Vue from "vue"
import app from "./app.vue"
// import {Header,Swipe,SwipeItem,Button,Lazyload} from "mint-ui"
import "./lib/mui/css/mui.css"
import "./lib/mui/css/icons-extra.css"
import VueRouter from "vue-router"
//导入自己的router.js路由模块
import router from "./router.js"
import VueResource from "vue-resource"
import Moment from "moment"
Vue.use(VueResource)
Vue.http.options.root="http://192.168.4.121:8080/weapp/vue"
Vue.use(VueRouter)
import Vuex from "vuex"
Vue.use(Vuex)
// Vue.component(Header.name,Header)
// Vue.component(Swipe.name,Swipe)
// Vue.component(SwipeItem.name,SwipeItem)
// Vue.component(Button.name,Button)
// Vue.use(Lazyload);
import MintUI from "mint-ui"
Vue.use(MintUI)
import "mint-ui/lib/style.css"
import VuePreview from "vue-preview"
Vue.use(VuePreview, {
    mainClass: 'pswp--minimal--dark',
    barsSize: {top: 0, bottom: 0},
    captionEl: false,
    fullscreenEl: false,
    shareEl: false,
    bgOpacity: 0.85,
    tapToClose: true,
    tapToToggleControls: false
  })
Vue.filter("dataFormat",function(dataStr,pattern="YYYY-MM-DD HH:mm:ss"){
    return Moment(dataStr).format(pattern)
})
var car=JSON.parse(localStorage.getItem("car")||'[]')
var store=new Vuex.Store({
    state:{
        car:car
    },
    mutations:{
        addgood(state,goodinfo){
            var flag=false;
            //点击加入购物车保存到全局
            state.car.some(item=>{
                if(item.id==goodinfo.id){
                    item.count+=parseInt(goodinfo.count);
                    flag=true;
                    return true;
                }
            })
            if(flag==false){
                state.car.push(goodinfo)
            }
            localStorage.setItem("car",JSON.stringify(state.car))
        },
        updategood(state,goodObj){
            state.car.some(item=>{
                if(goodObj.id==item.id){
                    item.count=parseInt(goodObj.count);
                    return true;
                }
            })
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        deletegood(state,id){
            state.car.some((item,i)=>{
                if(item.id==id){
                    state.car.splice(i,1)
                    return true;
                }
            })
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        updateSelect(state,stateObj){
            state.car.some(item=>{
                if(item.id==stateObj.id){
                    state.car.selected=stateObj.state
                }
            })
            localStorage.setItem("car",JSON.stringify(state.car))
        }
    },
    getters:{
        //相当于计算属性
        getAllCount(state){
            var sum=0;
            state.car.forEach(item=>{
                 sum+=item.count;
            })
            return sum;
        },
        getSelectGoodNum(state){
            var sum=0;
            state.car.forEach(item=>{
                if(item.selected){
                    sum+=item.count;
                }
            })
            return sum;
        },
        getSelectGoodPrice(state){
            var sum=0;
            state.car.forEach(item=>{
                if(item.selected){
                    sum+=item.count*item.price
                }
            })
            return sum;
        }
    }
})
var vm=new Vue({
    el:"#app",
    render(createElement){
        return createElement(app)
    },
    router,
    store:store
})